<?php

class Khachhang extends CI_Controller{
    function __construct() {
        parent::__construct();
        $this->load->model('user');
        $this->load->helper(array('form','url'));
    }
    function khachhang_page() {
        if ($this->user->check_user())
        {
            $this->load_html('nhanvien/khachhang_detail_view');
        }
    }
    function load_khachhang() {
        if ($this->user->check_user())
        {
            $MSKH=$this->input->post('MSKH');
            $this->load->model('khachhang_model');
            $row=$this->khachhang_model->load_by_id($MSKH)->row_array();
            if (count($row)>0){
                $this->load->library('table');
                $this->table->set_empty("&nbsp;");
                $this->table->set_template(array('table_open'=>'<table class="table">'));
                switch ($row['LOAIVE']) {
                            case 'VIP':
                                $row['LOAIVE']='Business';
                                break;
                            case 'SAV':
                                $row['LOAIVE']='Saver Flex';
                                break;
                            default:
                                $row['LOAIVE']='Economy';
                                break;
                        }
                    $PHIHH=$row['KHOILUONGVUOT']*$row['DONGIA_KG'];
                    if (substr($row['MSKH'], 0,2)=='NL')
                    {    
                        $this->table->set_heading('MSKH','Số ghế','Họ Tên','Ngày sinh','GT','Số ĐT','Địa chỉ','QT','CMND','Passport','TTTG', 'Loại vé','KLvượt','Phí HH');
                        $this->table->add_row($row['MSKH'],$row['GHESO'],$row['HOTEN'],date_format(date_create_from_format('d-M-y',$row['NGAYSINH']),'d/m/Y'),$row['GIOITINH'],$row['SODT'],$row['DIACHI'],$row['QUOCTICH'],$row['CMND'],$row['PASSPORT'],$row['TENTT'],$row['LOAIVE'],$row['KHOILUONGVUOT'],$PHIHH.' VNĐ');
                    }else{
                        $this->table->set_heading('MSKH','Số ghế','Họ Tên','Ngày sinh','GT','Số ĐT','Địa chỉ','QT','Người GH','TTTG', 'Loại vé','KLvượt','Phí HH');
                        $this->table->add_row($row['MSKH'],$row['GHESO'],$row['HOTEN'],date_format(date_create_from_format('d-M-y',$row['NGAYSINH']),'d/m/Y'),$row['GIOITINH'],$row['SODT'],$row['DIACHI'],$row['QUOCTICH'],$row['MSNGH'],$row['TENTT'],$row['LOAIVE'],$row['KHOILUONGVUOT'],$PHIHH.' VNĐ');
                    }
                $table = $this->table->generate();
                echo $table;
            }
        }
    
     }
     function Ganghe_page(){
        if ($this->user->check_user())
        {
            $data = array();
            $data['error'] = NULL;
            $this->load_html('nhanvien/ganghe_KH_view', $data);
        }
    }
    
    function search_KH(){
        if ($this->user->check_user())
        {
            $MSKH = $this->input->post('MSKH');
            
            $this->load->model('khachhang_model');
            $this->load->model('chuyenbay_model');
            
            $thongtin_KH['query'] = $this->khachhang_model->load_by_id($MSKH,FALSE)->row();
            $thongtin_KH['ghetrong'] = $this->chuyenbay_model->load_ghetrong_loaighe($thongtin_KH['query']->MSCB,$thongtin_KH['query']->LOAIVE)->result_array();
            echo $this->load->view('nhanvien/cap_nhat_ghe_view', $thongtin_KH, TRUE);
        }
    }
    
    function update_ghe(){
        if ($this->user->check_user())
        {
            $this->load->helper('form');
            $ghe = $this->input->post('ganghe');
            $MSKH = $this->input->post('MSKH');
            $MSCB=$this->input->post('MSCB');
            $this->load->model('ghekhach_model');
            $this->ghekhach_model->update($MSKH,$MSCB,$ghe);
            echo $ghe;
        }
    }
    function update_KLHH_page() {
        if ($this->user->check_user())
        {
            $this->load_html('nhanvien/update_KLHH_view');
        }
    }
    function update_KLHH() {
        if ($this->user->check_user())
        {
            $MSKH=$this->input->post('MSKH');
            $KLHH=$this->input->post('KLHH');
            $this->load->model('khachhang_model');

            $check=$this->khachhang_model->load_by_id($MSKH,FALSE)->row_array();
            if (count($check)<=0)
            {
                echo '<div id="attention" class="panel panel-danger">
                    <div class="panel-heading">Khách hàng không tồn tại</div>
                    </div>';
                return;
            }
            $this->load->model('phivchh_model');
            $PHIHH=$this->phivchh_model->load_by_id($check['MSPHH'])->row_array();
            
            $KLvuot=$KLHH-$PHIHH['TRONGLUONGDM'];
            $KLvuot=($KLvuot>0)?$KLvuot:0;
            $cost=$KLvuot*$PHIHH['DONGIA_KG'];
            
            $this->khachhang_model->update_KLHH($MSKH,$KLvuot);

                    $this->load->library('table');
                    $this->table->set_empty("&nbsp;");
                    $this->table->set_template(array('table_open'=>'<table class="table">'));
                    $this->table->set_heading('MSKH','Họ Tên','Khối lượng vượt','Phí');
                    $this->table->add_row($check['MSKH'],$check['HOTEN'],$KLvuot,$cost.' VNĐ');
                    $table = $this->table->generate();
                    $panel_header='<div class="panel panel-success">
                                    <div class="panel-heading">
                                        Cập nhật thành công
                                    </div>
                                    <div class="panel-body">';
                    $panel_footer='</div></div>';
                    echo $panel_header.$table.$panel_footer;
        }
    }
     function load_html($path,$data=NULL) {
        $this->load->view('common/nhanvien/header_view',$data);
        $this->load->view($path,$data);
        $this->load->view('common/nhanvien/footer_view',$data);
        
    }
}

